package com.zhouyu.service;

import java.util.Optional;

/**
 * JDK8 Optional test
 *
 * @author by gang.Li 2022/4/5 15:56
 */
public class OptionalTest {
	public static void main(String[] args) {
		//optional
//		Optional<Car> carOptional = Optional.empty();

//		Optional<Car> carOptional = Optional.of(new Car());
		Car car = new Car();
//		car.setName("xiaogang");
		Optional<Car> carOptional = Optional.ofNullable(car);

		// Optional 支持使用流式编程
		Optional<String> s = carOptional.map(Car::getName);

//		System.out.println(s.get());
		System.out.println(s.orElseGet(() -> "xiaoxiaogang"));
	}
}

class Car {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
